/*

Input: newdata/estimation_prep_ltw18 [prepared precinct-level panel]

Output: Table C.5, C.6 

Task: Sensitivity to inclusion of covariates


*/	
	
* PULL: Precinct-level data
	use "$newdata/estimation_prep_ltw18.dta", clear
	
********************************************************************************
		// Prep Estimation //
********************************************************************************
	
					
	// controls to be interacted with time dummies	
	global intctr	ln_ew_ges ew_biodt ew_dtmihi ew_ledig ew_married area_sb_ltw18 ///
				 wb_anteil  wb_ausl wb_18t24 wb_25t34 wb_35t44 wb_45t59 avg_dur hh_kids mpreis_flats_rent 	///
				 street_dist
				 
				
	// gen 2013 constant variables: suffix _2013
	foreach v of varlist $intctr {
		gen `v'_2013 = `v' if wahl_id==1
		grconst `v'_2013, by(sb_new) fill
	}
	
	// compute group ids for DISTANCE increase/decrease, 0 else
	gen 	tmp = (del_street_dist>0) 			if K==0
	bys sb_new (tmp): gen ind_dist_up = tmp[1]
	replace ind_dist_up = 0 					if missing(Ei)
	lab var ind_dist_up "=1 if dist increase in event, 0 else"
	
	cap drop tmp*
	gen 	tmp = (del_street_dist<0) 			if K==0
	bys sb_new (tmp): gen ind_dist_dn = tmp[1]
	replace ind_dist_dn = 0 					if missing(Ei)
	lab var ind_dist_dn "=1 if dist decrease in event, 0 else"		
	
	// Relabel outcomes for tables 
	lab var turnout_urne	"Polling place turnout"
	lab var turnout_pos_req "Mail-in turnout"
	lab var turnout_tot_req "Total turnout"
	
	
* TWFE OLS: gen leads and lags
	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen 	F`l'event = K==-`l'
		lab var F`l'event "Reassignment (#t-`l'#)"
	}	
	forvalues l = 0/7 {
		gen 	L`l'event = K==`l'
		lab var L`l'event "Reassignment (#t+`l'#)"
	}
	
	
* TWFE OLS: gen leads and lags by distance
	// Create two set of dummies: dist dummy x event-time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_a = F`l'event *ind_dist_dn				// a := decrease
		lab var F`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen	 	F`l'event_b = F`l'event *ind_dist_up				// b:= increase
		lab var F`l'event_b "(N+)x\hspace{.7cm}Reassignment (#t-`l'#)"

		assert  F`l'event_b+F`l'event_a==F`l'event		
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_a = L`l'event *ind_dist_dn	// a := decrease
		lab var L`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen	 	L`l'event_b = L`l'event *ind_dist_up	// b:= increase
		lab var L`l'event_b "(N+)x\hspace{.7cm}Reassignment (#t+`l'#)"
		assert  L`l'event_b+L`l'event_a==L`l'event
	}	
		

	
	
********************************************************************************
		// Baseline Specification: Robustness to inclusion of covariates //
********************************************************************************	


	order F1event, last
		
	global order  F4event F3event F2event /*F1event*/ L0event L1event L2event		

	estimates clear
	outreg, clear 
	
	local j=0
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
		
		//  NO CONTROLS
		local `++j'
		 reghdfe `v' F7event-L7event F1event /*$ctr*/ $wgt if smpl_trim==1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
		
		estimates store `v'_noctr
		qui outreg,  $opt  keep($order)  ctitle("", "(`j')") addrow(Time-varying controls, none )	
	
		
	// SOME TIME-VARYING CONTROLS (not in graph output only table)				
		local `++j'
		local somectr_1= "ln_ew_ges wb_anteil ew_biodt ew_dtmihi " 
		 reghdfe `v' F7event-L7event F1event `somectr_1' $wgt if smpl_trim==1, absorb(i.wahl_id#i.stadtbez i.sb_new ) cluster(sb_new)
		
		estimates store `v'_some1
		qui outreg,  $opt  keep($order)  ctitle("", "(`j')") addrow(Time-varying controls, some )
			
		
	// ALL TIME-VARYING CONTROLS (Baseline)
		local `++j'
		 reghdfe `v' F7event-L7event F1event $ctr $wgt if smpl_trim==1, absorb(i.wahl_id#i.stadtbez i.sb_new ) cluster(sb_new)
		
		estimates store `v'_bsl
		qui outreg,  $opt  keep($order)  ctitle("", "(`j')" \"", "`:var lab `v''") addrow(Time-varying controls, all )
	
	
	// PRE-TREATMENT CONTROLS (2013) x election FE; excluding units treated in 2013 (no time-varying controls)
		 local `++j'
		  reghdfe `v' F7event-L7event F1event /*$ctr*/  if smpl_trim==1 & Ei!=1, absorb(i.wahl_id#i.stadtbez i.sb_new c.(*_2013)##i.wahl_id) cluster(sb_new)
		
		estimates store `v'_int
		qui outreg,  $opt  keep($order)  ctitle("", "(`j')") addrow(Time-varying controls, none \ Pre-treatment covariates x election FE, yes )
	}	
	
	
	* TABLE C5. Robustness to Inclusion of Covariates–Pooled Reassignments
	outreg using "$tables/Table_C5_ES_rob_covariates_bsl", replay tex replace fragment note("some:`somectr_1'; all: $ctr; interacted: $intctr")
	cleantex     "$tables/Table_C5_ES_rob_covariates_bsl.tex"  ,  replace
	
********************************************************************************
		// Effects by distance: Robustness to inclusion of covariates //
********************************************************************************		
		
	
	// ORDER dummies
	order *event_b, last
	order F1event*,last	
	
	// Estimate ES by increase/decrease in distance
	estimates clear
	outreg,clear
	
	local j=0 // column counter
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
	// No Controls
	local j `++j' 
		 reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b /*$ctr*/ $wgt ///
					if  smpl_trim==1 , absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
				 qui outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b) ctitle("","(`j')" \ "", "") ///
					addrow(Time-varying controls, none)


		estimates store `v'_a_noctr
		estimates store `v'_b_noctr	
		
		
	// SOME TIME-VARYING CONTROLS (not in graph output only table)				
	local somectr_1= "ln_ew_ges wb_anteil ew_biodt ew_dtmihi " 
	local j `++j' 
		 reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b `somectr_1' $wgt ///
					if  smpl_trim==1 , absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
				qui outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b) ctitle("","(`j')" \ "", "") addrow(Time-varying controls, some)


		estimates store `v'_a_noctr
		estimates store `v'_b_noctr			
		
		
	// ALL TIME-VARYING CONTROLS (Baseline)
	local j `++j'
		 reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b $ctr $wgt ///
					if  smpl_trim==1  , absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
				 qui outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b)  ctitle("", "(`j')" \"", "`:var lab `v''") ///
					addrow(Time-varying controls,all)


		estimates store `v'_a_bsl
		estimates store `v'_b_bsl
			
	
	// INTERACTTION: pre-treatment control x election FE (excluding treated units in 2013); NO time-varying controls
	local j `++j'
		 reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b /*$ctr*/ $wgt ///
					if  smpl_trim==1 & Ei!=1 , absorb(i.wahl_id#i.stadtbez i.sb_new c.(*_2013)##i.wahl_id) cluster(sb_new) 
				qui  outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b) ctitle("","(`j')" \ "","") ///
					addrow(Time-varying controls, none \ pre-treatment covariates x election FE, yes)


		estimates store `v'_a_int
		estimates store `v'_b_int	
	}		
	
	
	* TABLE C6. Robustness to Inclusion of Covariates–Effects by Distance
	outreg using "$tables/Table_C6_ES_rob_covariates_dist2", replay tex replace fragment ///
		note("some:`somectr_1'; all: $ctr; interacted: $intctr")
	cleantex     "$tables/Table_C6_ES_rob_covariates_dist2.tex"  ,  replace
	